package com.mt.api.modules.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.api.core.page.PageFactory;
import com.mt.api.modules.domain.bo.CargoPageBo;
import com.mt.api.modules.domain.bo.CargoReportBo;
import com.mt.api.modules.domain.dto.CargoSalesReportDto;
import com.mt.api.modules.manager.ICargoManager;
import com.mt.api.constants.constant.Constants;
import com.mt.api.modules.domain.AppCargo;
import com.mt.api.modules.mapper.AppCargoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @Author: plover
 * @Date: 2022-01-02
 * @Description:
 */
@Component
public class CargoManagerImpl extends ServiceImpl<AppCargoMapper, AppCargo> implements ICargoManager {

    @Override
    public Page<AppCargo> getCargoPage(CargoPageBo bo) {
        Page page = PageFactory.defaultPage(bo);
        LambdaQueryWrapper<AppCargo> wrapper = Wrappers.lambdaQuery();
        wrapper.nested(StringUtils.isNotEmpty(bo.getSearchVal()), queryWrapper -> queryWrapper.like(AppCargo::getCargoNo, bo.getSearchVal())
                .or().like(AppCargo::getCargoName, bo.getSearchVal()))
                .eq(Objects.nonNull(bo.getStatus()), AppCargo::getStatus, bo.getStatus())
                .orderByDesc(AppCargo::getCargoNo);
        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public AppCargo getCargoByCargoNo(String cargoNo) {
        LambdaQueryWrapper<AppCargo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AppCargo::getCargoNo, cargoNo)
                .eq(AppCargo::getStatus, Constants.SaleState.ON)
                .last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<AppCargo> getCargoByBrandId() {
        LambdaQueryWrapper<AppCargo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AppCargo::getStatus, Constants.SaleState.ON)
                .orderByDesc(AppCargo::getWeight);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<CargoSalesReportDto> getSalesDayStat(CargoReportBo bo) {
        return baseMapper.getSalesDayStat(bo);
    }

}
